/**
 * 
 */
package com.wolfware.athsweb.manager.impl;

import java.util.Date;
import java.util.List;

import com.wolfware.athsweb.domain.AbstractEvent;
import com.wolfware.athsweb.domain.Competition;
import com.wolfware.athsweb.factory.CompetitionFactory;
import com.wolfware.athsweb.manager.CompetitionEventManager;
import com.wolfware.athsweb.persistancy.dao.CompetitionDAO;
import com.wolfware.athsweb.response.Response;
import com.wolfware.athsweb.response.impl.AthsWebResponseImpl;

/**
 * @author ChristianStrzadala
 *
 */
public class CompetitionEventManagerImpl implements CompetitionEventManager {

	private CompetitionFactory competitionFactory;
	private CompetitionDAO competitionDAO;
	
	/**
	 * @param competitionDAO the competitionDAO to set
	 */
	public void setCompetitionDAO(final CompetitionDAO competitionDAO) {
		this.competitionDAO = competitionDAO;
	}

	/**
	 * @param competitionFactory the competitionFactory to set
	 */
	public void setCompetitionFactory(final CompetitionFactory competitionFactory) {
		this.competitionFactory = competitionFactory;
	}

	/**
	 * {@inheritDoc}
	 */
	public Response submitNewCompetition(String compName, Date compDate, List<AbstractEvent> compEventList) {
		Competition competition = competitionFactory.createCompetition(compName, compDate, compEventList);
		
		// Transaction management should be around this and catch more specific exception
		try {
			competitionDAO.saveCompetitionEntry(competition);	
		} catch (Exception ex) {
			
		}
		
		return new AthsWebResponseImpl();
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void addEventToCompetition(Long compId, AbstractEvent compEvent) {
		Competition competition = competitionDAO.getCompetitionById(compId);
		
		competition.addEvent(compEvent);
		
		competitionDAO.updateCompetitionEntry(competition);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List<Competition> retrieveAllCompetitions() {
		return competitionDAO.getAllCompetitions();
	}

	/**
	 * {@inheritDoc}
	 */
	public void removeEventFromCompetition(Long compId, AbstractEvent compEvent) {
		Competition competition = competitionDAO.getCompetitionById(compId);
		
		competition.removeEvent(compEvent);
		
		competitionDAO.updateCompetitionEntry(competition);
	}

}